#!/usr/bin/python

# it compares sstable files known by Hypertable with files on disk

if '/opt/hypertable/current/lib' not in os.environ.get('LD_LIBRARY_PATH',''):
    os.system('LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/opt/hypertable/current/lib" python "%s"' % sys.argv[0])
    sys.exit()

import ht
from subprocess import *

client = ht.Client("/opt/hypertable/current/conf/hypertable.cfg")

def system(cmd):
    return Popen(cmd, shell=True, stdout=PIPE).stdout

# it compares sstable files known by Hypertable with files on disk

prevcell = None
files_in_use = set()
for cell in client.hql("select Files from METADATA"):

    if prevcell!=None and \
       cell.row_key == prevcell.row_key and \
       cell.column_family == prevcell.column_family and \
       cell.column_qualifier == prevcell.column_qualifier:
        continue
    prevcell = cell

    for filename in cell.value.split(';\n'):
        if not filename:continue
        files_in_use.add('/opt/hypertable/current/fs/local'+filename)

files_on_disk = set()
for filename in system('find /opt/hypertable/current/fs/local/hypertable/tables -type f'):
    files_on_disk.add( filename.strip() )


print 'Garbage files:'
for filename in (files_on_disk - files_in_use):
    print system('ls -l "%s"' % filename).read().strip()

print 'Missed files:'
for filename in (files_in_use - files_on_disk):
    print filename
